// components/u-authorize/index.js
const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        show: {
            type: Boolean,
            value: true
        },
        is_getphone: {
            type: Boolean,
            value: false
        },

    },

    /**
     * 组件的初始数据
     */
    data: {

    },

    /**
     * 组件的方法列表
     */
    methods: {
        onCancel(event) {
            this.triggerEvent('close');
        },
        onConfirm(event) {
            this.triggerEvent('confirm', event.detail);
        },
        // 获取用户信息
        bindGetUserInfo(e){
            var that = this;
            if (e.detail.errMsg == 'getUserInfo:fail auth deny'){
                wx.showModal({
                    title: '温馨提示',
                    content: '您已拒绝授权，无法获取用户信息！',
                    showCancel: false,
                    success: function(res){
                    if(res.confirm){
                        var user=app.globalData.userInfo
                        if(user.mobile){
                            that.triggerEvent('close', {});

                        }else{
                            that.setData({
                                is_getphone:true
                            })
                        }
                    }
                    }
                });
                return;
            } else {
                app.decryptUserInfo(e.detail, function () {
                    that.triggerEvent('close', {});
                });
            }
        },
        getUserProfile(e) {
            var that=this
            wx.getUserProfile({
                desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
                success: (res) => {
                console.log(res)
                // return false
                app.decryptUserInfo(res,function(ret){
                    var user=app.globalData.userInfo
                        if(user.mobile){
                            that.triggerEvent('close', {});

                        }else{
                            that.setData({
                                is_getphone:true
                            })
                        }
                    // that.triggerEvent('close', {});
                })
                // this.setData({
                //   userInfo: res.userInfo,
                //   hasUserInfo: true
                // })
                }
            })
        },
        // 获取手机号
        bindGetPhoneNumber(e){
            var that = this;
            
            if (e.detail.errMsg == 'getPhoneNumber:fail user deny'){
                wx.showModal({
                    title: '温馨提示',
                    content: '您已拒绝授权，无法获取手机号！',
                    showCancel: false,
                    success: function(res){
                    if(res.confirm){
                        that.triggerEvent('close');
                        
                        // that.triggerEvent('close', {});
                    }
                    }
                });
                return;
            } else {
                app.decryptPhoneNumber(e.detail, function () {
                    that.triggerEvent('close');
                   
                    
                    // that.triggerEvent('close', {});
                });
            }
        },


    }
})
